;(function() {
	'use strict';
	angular.module('controllers.sysServiceData.uPayCommissionRate')
		.controller('uPayCommissionRateController', uPayCommissionRateController);

	function uPayCommissionRateController($scope, pBossSuccessRate, COREDATA, uPayCommissionRateService, CoreService) {
		//控制查询条件拉上拉下   1为拉下  0 为拉上
		$scope.shoumun = {
			num: 1
		};
		//控制等待样式   false 为隐藏   true 为显示
		$scope._loading.show = false;
		$scope.timeTypes = pBossSuccessRate.getTimeType();
		$scope.sendData = uPayCommissionRateService.getPageBind();
		$scope.formPrefix = COREDATA.FORMPREFIX + 'sysServiceData/uPay/upayCSAExcelExport';
		var year = new Date().getFullYear();
		var month = new Date().getMonth();
		$scope.$watch('sendData.timeType', function(newValue) {
			if (newValue === '3') {
				$scope.sendData.startTime = new Date(year, month);
				$scope.sendData.endTime = new Date(year, month);
			} else {
				$scope.sendData.startTime = Number(year);
				$scope.sendData.endTime = Number(year);
			}
		});
		var myEcharts = echarts.init(document.getElementById('doc'));
		$scope.search = function() {
			if ($scope.shoumun.num === 1) {
				$scope.shoumun.num = 1;
				return;
			}
			$scope.send = {};
			$scope.send.timeType = $scope.sendData.timeType;
			if ($scope.sendData.timeType === '3') {
				$scope.send.startTime = CoreService.dateFormat($scope.sendData.startTime, 'yyyy-MM');
				$scope.send.endTime = CoreService.dateFormat($scope.sendData.endTime, 'yyyy-MM');
			} else {
				$scope.send.startTime = $scope.sendData.startTime + '';
				$scope.send.endTime = $scope.sendData.endTime + '';
			}
			if ($scope.send.startTime === $scope.send.endTime) {
				$scope.title = $scope.send.startTime.replace(/-/g, '') + '统一支付佣金结算业务有效投诉与总投诉对比';
			} else {
				$scope.title = $scope.send.startTime.replace(/-/g, '') + '_' + $scope.send.endTime.replace(/-/g, '') + '统一支付佣金结算业务有效投诉与总投诉对比';
			}
			$scope.send.title = $scope.title;
			uPayCommissionRateService.queryScoreDet($scope.send, function(res) {
				if (res.state === '10000') {
					$scope.items = res.data;
					myEcharts.clear();
					var myOption = uPayCommissionRateService.getEchartsData(res.data, $scope.title);
					myEcharts.setOption(myOption);
					exportView();
					$scope.flag = 'view';
				} else if (res.data.length === 0) {
					$scope.shoumun.num = 1;
					CoreService.dialog('查询无数据');
				} else {
					$scope.shoumun.num = 1;
					CoreService.dialog('查询错误');
				}
			});
		};
		$scope.showView = function() {
			$scope.flag = 'view';
		};
		$scope.showTable = function() {
			$scope.flag = 'table';
		};
		$scope.sendDown = {
			page:{
				currentPage:1,
				pageSize:'10'
			}
		};
		$scope.tableDown = function(item) {
			$scope.sendDown = {
				page:{
					currentPage:1,
					pageSize:'10'
				},
				startTime: item.time
			};
			$scope.downTitle = item.time.replace(/-/g,'')+'统一支付佣金结算业务详细信息';
			$scope.queryDownData();
		};
		$scope.queryDownData = function(){
			uPayCommissionRateService.queryScoreDetTable($scope.sendDown, function(res) {
				if (res.state === '10000') {
					$scope.downData = ypaging($scope.sendDown,res.data);
					$scope.totalCount = res.data.length;
					$scope.flag = 'tableDown';
				} else {
					CoreService.dialog('查询无数据');
				}
			});
		};
		$scope.back = function() {
			$scope.flag = 'table';
		};
		//导出excel
		function exportView() {
			$scope.outData = JSON.stringify($scope.send);
		}

		function ypaging(reqBody, resData) {
			var page = reqBody.page;
			var data = {
				totalCount: resData.length,
				lineData: []
			};
			for (var currRow = (page.currentPage - 1) * page.pageSize; currRow < resData.length && currRow < page.currentPage * page.pageSize; currRow++) {
				data.lineData.push(resData[currRow]);
			}
			return data;
		}
	}
	uPayCommissionRateController.$inject = ['$scope', 'pBossSuccessRate', 'COREDATA', 'uPayCommissionRateService', 'CoreService'];
})();